package com.qf.service.impl;

import com.qf.dao.repository.RotationRepository;
import com.qf.pojo.resp.BaseResp;
import com.qf.pojo.vo.Rotation;
import com.qf.service.RotationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class RotationServiceImpl implements RotationService {

    @Autowired
    RotationRepository rotationRepository;

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public BaseResp findAll() {
       //查询redis中是否有数据
        Boolean rotation_list = redisTemplate.hasKey("rotation_list2");
        Set rotation_list1 =null;
        if (!rotation_list){
            //到数据库查询
            List<Rotation> start = rotationRepository.findByStatus("start");
            for (Rotation r:start){
                redisTemplate.opsForZSet().add("rotation_list2",r,r.getIndex());
            }
            rotation_list1 = redisTemplate.opsForZSet().reverseRange("rotation_list2", 0, -1);
        }
        rotation_list1 = redisTemplate.opsForZSet().reverseRange("rotation_list2", 0, -1);
        return new BaseResp(0,"查询成功",null,rotation_list1);
    }
}
